package com.wztk.shydemo.proxy;

import android.util.Log;
import android.widget.Toast;
import com.wztk.shydemo.MainActivity;

/**
 * Created by win7 on 2016/7/8.
 */
public class RedsocksCmds {
    private final static String REDSOCKS_START_SHELL = "redsocks_startup.sh";
    private final static String REDSOCKS_STOP_SHELL = "redsocks_stop.sh";
    private final static String REDSOCKS = "redsocks";
    private final static String IPTABLES = "iptables";
    private final static String CNTLM = "cntlm";
    private final static String TPROXY = "tproxy";
    private final static String STUNNEL = "stunnel";

    private String workdir;
    private String uid;
    private String server;
    private String serverPort;
    private String port1;
    private String port2;

    public RedsocksCmds(String workdir, String uid, String server, String serverPort,String port1, String port2){
        this.workdir = workdir;
        this.uid = uid;
        this.server = server;
        this.serverPort = serverPort;
        this.port1 = port1;
        this.port2 = port2;
    }

    private void chmodExefile() {
        String cmds = "chmod 700 " +  this.workdir + this.IPTABLES + "\n" +
                "chmod 700 " +  this.workdir + this.REDSOCKS + "\n" +
                "chmod 700 " +  this.workdir + this.CNTLM + "\n" +
                "chmod 700 " +  this.workdir + this.TPROXY + "\n" +
                "chmod 700 " +  this.workdir + this.STUNNEL + "\n";
        ShellUtils.execCommand(cmds, true);
    }

    public void startRedsocks(){
        chmodExefile();
        createRedsocksCfg();
        createRedsocksStartShell();
        createRedsocksStopShell();
        stopRedsocks(this.workdir);     //不管程序是否运已经行起来先KILL一遍
        String cmds = "sh " + this.workdir + "/" + this.REDSOCKS_START_SHELL;
        ShellUtils.execCommand(cmds, true);
    }

    public static void stopRedsocks(String workdir){
        String cmds = "sh " + workdir + "/" + REDSOCKS_STOP_SHELL;
        ShellUtils.CommandResult cr =  ShellUtils.execCommand(cmds, true);

        //Log.i("shy", cr.successMsg);
        Log.i("shy", cr.errorMsg);

    }

    private void createRedsocksStartShell(){
        String cmds = "echo \"" + "#!/system/bin/sh\n\n" +
                this.workdir + "/" + this.REDSOCKS + " -p " + this.workdir + "/redsocks.pid -c " + this.workdir + "/redsocks.conf\n" +
                this.workdir + "/" + this.IPTABLES + " -t nat -A OUTPUT -p tcp -d " + this.server + " -j RETURN\n" +
                this.workdir + "/" + this.IPTABLES + " -t nat -m owner --uid-owner " + this.uid + " -A OUTPUT -p tcp --dport 80 -j REDIRECT --to " + this.port1 + "\n" +
                this.workdir + "/" + this.IPTABLES + " -t nat -m owner --uid-owner " + this.uid + " -A OUTPUT -p tcp --dport 443 -j REDIRECT --to " + this.port2 + "\n" +
                this.workdir + "/" + this.IPTABLES + " -t nat -m owner --uid-owner " + this.uid + " -A OUTPUT -p tcp --dport 5228 -j REDIRECT --to " + this.port2 + "\"";
        cmds = cmds + " > " + this.workdir + "/" + this.REDSOCKS_START_SHELL;
        ShellUtils.execCommand(cmds, true);
    }

    private void createRedsocksStopShell(){
        String cmds = "echo \"" + "#!/system/bin/sh\n\n" +
                this.workdir + "/" + this.IPTABLES + " -t nat -D OUTPUT -p tcp -d " + this.server + " -j RETURN\n" +
                this.workdir + "/" + this.IPTABLES + " -t nat -m owner --uid-owner " + this.uid + " -D OUTPUT -p tcp --dport 80 -j REDIRECT --to " + this.port1 + "\n" +
                this.workdir + "/" + this.IPTABLES + " -t nat -m owner --uid-owner " + this.uid + " -D OUTPUT -p tcp --dport 443 -j REDIRECT --to " + this.port2 + "\n" +
                this.workdir + "/" + this.IPTABLES + " -t nat -m owner --uid-owner " + this.uid + " -D OUTPUT -p tcp --dport 5228 -j REDIRECT --to " + this.port2 + "\n" +
                "kill -9 \\`cat " + this.workdir + "/redsocks.pid\\`\n" +
                "rm " + this.workdir + "/redsocks.pid" + "\"";
        cmds = cmds + " > " + this.workdir + "/" + this.REDSOCKS_STOP_SHELL;
        ShellUtils.execCommand(cmds, true);
    }

    public void createRedsocksCfg(){
        String cmds = "echo \"";
        cmds = cmds +
                "base {\n" +
                "    log_debug = off;\n" +
                "    log_info = off;\n" +
                "    log = stderr;\n" +
                "    daemon = on;\n" +
                "    redirector = iptables;\n" +
                "}\n" +
                "redsocks {\n" +
                "    local_ip = 127.0.0.1;\n" +
                "    local_port = 8123;\n" +
                "    ip = " + this.server + ";\n" +
                "    port = " + this.serverPort + ";\n" +
                "    type = http-relay;\n" +
                "} \n" +
                "redsocks {\n" +
                "    local_ip = 127.0.0.1;\n" +
                "    local_port = 8124;\n" +
                "    ip = " + this.server + ";\n" +
                "    port = " + this.serverPort + ";\n" +
                "    type = http-connect;\n" +
                "}\"";
        cmds = cmds + " > " + workdir + "/redsocks.conf";
        ShellUtils.execCommand(cmds, true);
    }
}
